package com.rephael.van.base;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

/**
 * Activity基础类，用来封装常用方法
 * Created by rephael on 15-5-27.
 */
public class BaseActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 获取获取控件对象的公共方法
     * @param resId 控件资源id
     * @param <T>
     * @return
     */
    protected <T extends View> T getView(int resId){
        return (T)findViewById(resId);
    }

    /**
     * 获取控件对象，适用于外部引入的View
     * @param view
     * @param resId
     * @param <T>
     * @return
     */
    protected <T extends View> T getView(View view, int resId){
        return (T)view.findViewById(resId);
    }

    //获取控件
    protected void findViews(){

    }
    //设置事件监听
    protected void setListener(){

    }
    //数据初始化
    protected void initData(){

    }

    /**
     * 显示Toast
     * @param content
     */
    protected void showShortToast(String content){
        Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
    }

    /**
     * 显示Toast,参数为id
     * @param resId
     */
    protected void showShortToast(int resId){
        Toast.makeText(this, getResources().getText(resId), Toast.LENGTH_SHORT).show();
    }

    /**
     * 显示长时间Toast
     * @param content　要显示的字符串
     */
    protected void showLongToast(String content){
        Toast.makeText(this, content, Toast.LENGTH_LONG).show();
    }

    /**
     *
     * @param resId　资源id
     */
    protected void showLongToast(int resId){
        Toast.makeText(this, getResources().getText(resId), Toast.LENGTH_LONG).show();
    }

    /**
     * 保存字符串到xml
     * @param fileName
     * @param key
     * @param value
     */
    protected void writeSharedPreferbces(String fileName, String key, String value){
        SharedPreferences preferences = getSharedPreferences(fileName, MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * 保存整数到xml
     * @param fileName
     * @param key
     * @param value
     */
    protected  void writeSharedPreferences(String fileName, String key, int value){
        SharedPreferences preferences = getSharedPreferences(fileName, MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    /**
     * 保存布尔值到xml
     * @param fileName
     * @param key
     * @param value
     */
    protected void writeSharedPrefernces(String fileName, String key, boolean value){
        SharedPreferences preferences = getSharedPreferences(fileName, MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    /**
     * 从xml中读取字符串
     * @param fileName
     * @param key
     * @param defaultValue
     * @return
     */
    protected String readSharedPreferences(String fileName, String key, String defaultValue){
        SharedPreferences preferences = getSharedPreferences(fileName, MODE_PRIVATE);
        return preferences.getString(key, defaultValue);
    }

    /**
     * 从xml中读取整数
     * @param fileName
     * @param key
     * @param defaultValue
     * @return
     */
    protected int readSharedPreferences(String fileName, String key, int defaultValue){
        SharedPreferences preferences = getSharedPreferences(fileName, MODE_PRIVATE);
        return preferences.getInt(key, defaultValue);
    }

    /**
     * 从xml中读取布尔值
     * @param fileName
     * @param key
     * @param defaultValue
     * @return
     */
    protected boolean readSharedPreferences(String fileName, String key, boolean defaultValue){
        SharedPreferences preferences = getSharedPreferences(fileName, MODE_PRIVATE);
        return preferences.getBoolean(key, defaultValue);
    }
}
